/**
 * Team
 */

$(document).ready(function(){
});



/**
 * Подача/отмена запроса на набор игроков в команду
 */
if ( typeof(teamChangeVacancyStatus) != "function" ){
function teamChangeVacancyStatus(area, team_id, mode)
{
	if ( !team_id ) return false;
	
	var rq_url = "/common/change_vacancy_status.php?team_id="+team_id+'&mode='+(mode=='add'?'add':'del');
	
	// Делаем синхронный запрос
	var isVacancy  = 
		$.ajax({
			type: "GET",
			url: rq_url,
			async: false
		}).responseText;

	if ( mode=='add' && isVacancy ){
		$("#addVacancy"+area+"").hide();
		$("#delVacancy"+area+"").show();
	}else{
		$("#delVacancy"+area+"").hide();
		$("#addVacancy"+area+"").show();
	}
	return true;
}
}


/**
 * Октрытие формы для смены капитана
 */
if ( typeof(teamOpenFormAssignCaptain) != "function" ){
function teamOpenFormAssignCaptain(area, idShow, team_id)
{
	if ( !team_id ) return false;
	
	$.fancybox({
		'content': '<div class="form form-mw2">'+$("#"+idShow+"").html()+'</div>',
		'onClosed': function(){}
	});

	return true;
}
}


/**
 * Смена капитана
 */
if ( typeof(teamAssignCaptain) != "function" ){
function teamAssignCaptain(area, team_id, form)
{
	var show_error = function(msg){
		$('form[name="'+form.name+'"] #id_errorAssignCaptain'+area+'').html(msg).show();
	}
	var hide_error = function(){
		$('form[name="'+form.name+'"] #id_errorAssignCaptain'+area+'').html('').hide();
	}

	hide_error();
	
	if ( !confirm(Dictionary.translate('lang_team_assign_captain_confirm')) )
		return false;

	if ( !team_id ){
		show_error(Dictionary.translate('lang_common_system_error'));
		return false;
	}
	
	var footballer_id = 0;
	$('form[name="'+form.name+'"] input:radio:checked[name="FOOTBALLER_ID"]').each(function(){if (!footballer_id) footballer_id = $(this).val();});
	
	if ( !footballer_id ){
		show_error(Dictionary.translate('lang_team_assign_captain_empty'));
		return false;
	}

	var callback = function(data){
		$.fancybox({
			'content': '<div class="form form-mw">'+Dictionary.translate('lang_team_assign_captain_ok')+' '+data.new_captain_name+'</div>',
			'onClosed': function(){}
		});
		window.location.reload();
	}
	
	var mode = 'captain';
	var action = 'assign';
	var res = $.ajax({
		url:'/common/team.php?mode='+mode+'&action='+action,
		cache: false,
		data: {'id':team_id,'footballer_id':footballer_id},
		success:function(data){
			var response = (typeof data == 'undefined' || data.length==0) ? {} : eval( "(" + data + ")" );
			if (response.action == action){
				if (response.status){
					callback(response);
				}else{
					if ( response.error == 'noRight' ){
						show_error(Dictionary.translate('lang_team_error_noRight'));
					}else{
						show_error(Dictionary.translate('lang_team_error_other')+(response.body?':\r\n'+response.body:''));
					}
				}
			}
		}
	}); 	

	return false;
}
}

